package com.ruoyi.base.controller;

import com.ruoyi.base.domain.Room;
import com.ruoyi.base.domain.TreeNode;
import com.ruoyi.base.service.IRoomService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 房间档案Controller
 *
 * @author ruoyi
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/base/room")
@Api(tags = "房间档案")
public class RoomController extends BaseController {
    @Autowired
    private IRoomService roomService;

    /**
     * 查询房间档案列表
     */
    @GetMapping("/list")
    @ApiOperation("查询房间档案列表")
    public TableDataInfo<Room> list(Room room) {
        startPage();
        List<Room> list = roomService.selectRoomList(room);
        return getDataTable(list);
    }

    /**
     * 导出房间档案列表
     */
    @ApiOperation("导出房间档案列表")
    @Log(title = "房间档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Room room) {
        List<Room> list = roomService.selectRoomList(room);
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        util.exportExcel(response, list, "房间档案数据");
    }

    /**
     * 获取房间档案详细信息
     */
    @ApiOperation("获取房间档案详细信息")
    @GetMapping(value = "/{id}")
    public R<Room> getInfo(@PathVariable("id") String id) {
        return R.ok(roomService.selectRoomById(id));
    }

    /**
     * 新增房间档案
     */
    @PreAuthorize("@ss.hasPermi('base:room:add')")
    @ApiOperation("新增房间档案")
    @Log(title = "房间档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Room room) {
        return toAjax(roomService.insertRoom(room));
    }

    /**
     * 修改房间档案
     */
    @PreAuthorize("@ss.hasPermi('base:room:edit')")
    @ApiOperation("修改房间档案")
    @Log(title = "房间档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Room room) {
        return toAjax(roomService.updateRoom(room));
    }

    /**
     * 批量修改房间档案
     */
    @PreAuthorize("@ss.hasPermi('base:room:edit')")
    @ApiOperation("批量修改房间档案")
    @Log(title = "房间档案", businessType = BusinessType.UPDATE)
    @PutMapping("/batchEdit")
    public AjaxResult batchEdit(@RequestBody List<Room> rooms) {
        return toAjax(roomService.batchUpdateRoom(rooms));
    }

    /**
     * 删除房间档案
     */
    @PreAuthorize("@ss.hasPermi('base:room:remove')")
    @Log(title = "房间档案", businessType = BusinessType.DELETE)
    @ApiOperation("删除房间档案")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(roomService.deleteRoomByIds(ids));
    }

    /**
     * 查询房间树
     */
    @Log(title = "房间档案", businessType = BusinessType.DELETE)
    @ApiOperation("查询房间树")
    @GetMapping("/tree")
    public R<List<TreeNode>> tree(
            @ApiParam("要查询的列表层级-build：查询楼座；floor：楼层；room：房间") String name,
            @ApiParam("父节点的ID") String id) {
        return R.ok(roomService.getRoomTree(name, id));
    }

    /**
     * 批量设置单价
     */
    @PreAuthorize("@ss.hasPermi('base:room:batch:price')")
    @ApiOperation("批量设置单价")
    @Log(title = "房间档案", businessType = BusinessType.UPDATE)
    @PutMapping("/batchPrice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eqPrice", value = "售电单价", required = true)
    })
    public R<Integer> batchPrice(@RequestBody List<String> roomIds, BigDecimal eqPrice) {
        return R.ok(roomService.batchPrice(roomIds, eqPrice));
    }

    /**
     * 更换设备
     */
    @PreAuthorize("@ss.hasPermi('base:room:device:set')")
    @ApiOperation("更换设备")
    @Log(title = "房间档案", businessType = BusinessType.UPDATE)
    @PutMapping("/deviceSet")
    public R<Integer> setDevice(@RequestBody Room room) {
        return R.ok(roomService.deviceSet(room));
    }

}
